package com.zjs.jsp.mongodbapi.web;

import com.zjs.jsp.domain.jsp.Jsp_Task;
import com.zjs.jsp.mongodbapi.service.mongodb.JspTaskExpand;
import com.zjs.jsp.mongodbapi.service.mongodb.JspTaskRepository;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 李文
 * @create 2017-09-05 13:28
 **/
@RestController
public class MongodbApiController
{

    private static final Logger LOGGER = LoggerFactory.getLogger(MongodbApiController.class);

    @Autowired
    private JspTaskExpand jspTaskExpand;
    @Autowired
    private JspTaskRepository jspTaskRepository;


    @ApiOperation(value = "更新状态", notes = "通过 tk 更新任务类型  10 自动分配  20 抢单  30  强制分配")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tk", value = "任务编号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "rwlx", value = "任务类型", required = true, dataType = "Integer", paramType = "query")
    })
    @PostMapping("/updateRwlx")
    public Boolean updateRwlx(@RequestParam("tk") String tk, @RequestParam("rwlx") Integer rwlx) {
        try {
            LOGGER.info(tk + "  修改 rwlx 状态为  " + rwlx);
            if (jspTaskExpand.updateRwlx(tk, rwlx)) {
                LOGGER.info(tk + "  修改 rwlx 成功  " + rwlx);
                return true;
            } else {
                LOGGER.info(tk + "  修改 rwlx 失败  " + rwlx);
                return false;
            }
        } catch (Exception e) {
            LOGGER.error(tk + "  修改 rwlx 状态为  " + rwlx + "  异常 " + e.getMessage(), e);
            return false;
        }
    }

    @ApiOperation(value = "获取任务包数据", notes = "通过PK 获取任务包数据 ")
    @ApiImplicitParam(name = "tk", value = "任务编号", required = true, dataType = "String", paramType = "query")
    @GetMapping("/getJspTask")
    public List<Jsp_Task> getJspTask(@RequestParam("tk") String tk) {
        try {
            return jspTaskRepository.findByTk(tk);
        } catch (Exception e) {
            LOGGER.error("获取任务包数据异常 " + e.getMessage(), e);
        }
        return null;
    }


    @ApiOperation(value = "获取任务包数据", notes = "通过 订单号 获取任务包数据 ")
    @ApiImplicitParam(name = "poc", value = "订单号", required = true, dataType = "String", paramType = "query")
    @GetMapping("/getPoctoJspTask")
    public List<Jsp_Task> getPoctoJspTask(@RequestParam("poc") String poc) {
        try {
            return jspTaskRepository.findByOdPoc(poc);
        } catch (Exception e) {
            LOGGER.error("获取任务包数据异常 " + e.getMessage(), e);
        }
        return null;
    }
}
